import Vue from 'vue'
import Router from 'vue-router'
import menus from '@/config/menu-config'
import axios from 'axios'


Vue.use(Router)

var routes = [];

menus.forEach((item) => {
    item.sub.forEach((sub) => {
        routes.push({
            path: `/${sub.componentName}`,
            name: sub.componentName,
            component: () =>
                import (`@/components/${sub.componentName}`)
        })
    })
})

routes.push({
    path: '/login',
    name: 'login',
    component: () =>
        import (`@/components/login`)
});
var router = new Router({
    routes
})

router.beforeEach((to, from, next) => {
    axios.post('' + router.app.$options.store.state.domain + '/bzDCP/login/getuser', JSON.stringify({}))
        .then(response => {
            if (response.data.code == '200') {
                router.app.$options.store.state.loginStat = response.data;
                if (to.name == 'login') {
                    next({
                        path: '/DataScreen'
                    });
                } else {
                    next();
                }
            } else if (to.name != 'login') {
                next({
                    path: '/login',
                });
            } else {
                next();
            }
        })
});

export default router;